// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata out_mgrow2_3_fig_sum.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_201910/out_mgrow2_3_fig_sum_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_mgrow2/"
capture mkdir ${dir_outf}
capture mkdir ${dir_outf}/data/

global gl_geo = "msa fips czone msa1983 msa1983cz"

global sigma = 3
global rho = 2

global year1 = 1977
global year2 = 2013

// Load functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

//==============================================================================
// Figures

capture program drop cmgrow_fig
program cmgrow_fig
args v_city png_out i_in

local v_y = "cmgrow"

local x_min = "."
if "`v_city'"=="fips" {
/* REDACTED
Numbers specifying x/y axis ranges and steps redacted */
		local x_min = "REDACTED"
		local c_xlab = "xlab(REDACTED(REDACTED)REDACTED)"
	
}
else if "`v_city'"=="msa" {
		local x_min = "REDACTED"
		local c_xlab = "xlab(REDACTED(REDACTED)REDACTED)"
}
else if "`v_city'"=="czone" {
		local x_min = "REDACTED"
		local c_xlab = "xlab(REDACTED(REDACTED)REDACTED)"
}

local x_max = "."

local y_min = .
local y_max = .
local y_step = .


if "`v_city'" == "msa" {
/* REDACTED
Numbers specifying x/y axis ranges and steps redacted */
local y_min = "REDACTED"
local y_max = "REDACTED"
local y_step = "REDACTED"
}
if "`v_city'" == "fips" {
local y_min = "REDACTED"
local y_max = "REDACTED"
local y_step = "REDACTED"
} 
if inlist("`v_city'", "czone", "msa1983cz","msa1983") {
local y_min = "REDACTED"
local y_max = "REDACTED"
local y_step = "REDACTED"
} 


lpolyci_data "" "df_fig" "`v_y'" "ln_emp_city_y1" "99" "100" "" ""

frame df_fig {
	di "Generate Figures"
	local l_y = "`v_y'"
	
	preserve
	keep `v_y'_x `v_y'_s `v_y'_se
	rounding_4dig "`v_y'_x `v_y'_s `v_y'_se"
	*export excel "${dir_outf}/lpoly_mgrow.xlsx", sheet("`png_out'") sheetreplace firstrow(var) keepcellfmt
	restore
	
	foreach i_y in `l_y' {
		foreach i_line in s cil ciu x {
			replace `i_y'_`i_line' = . if ! inrange(`i_y'_x, `x_min', `x_max')
		}
		//replace `i_y'_cil = `y_min' if `i_y'_cil <= `y_min'
		//replace `i_y'_ciu = `y_max' if `i_y'_ciu >= `y_max'
	}
	
	local t_city = upper("`v_city'")
	if "`v_city'" == "msa" local t_sampcity = "JOB{1,5}_CINDNEW_M1F1_Y1_Y2"
	if "`v_city'" == "fips" local t_sampcity = "JOB{1,5}_CINDNEW_M*F1_Y1_Y2"
	if regexm("`i_in'", "1") local t_sample = "ALL, JOB{3,4}_Y1_Y2, JOB*_Y1_Y2"
	if regexm("`i_in'", "2") local t_sample = "ALL, JOB{3,4}_Y1_Y2, `t_sampcity', JOB*_Y1_Y2"
	
	local t_title "City Missing Growth"
	if regexm("`i_in'", "1") local t_subtitle "`t_city', Incumbent Industry (Unadjusted)"
	if regexm("`i_in'", "2") local t_subtitle "`t_city', Incumbent + New Industry (Adjusted)"
	
	twoway (rarea `v_y'_cil `v_y'_ciu `v_y'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
		(connected `v_y'_s `v_y'_x, m(i) lcolor(navy) lw(medthick)), ///
		ylabel(`y_min'(`y_step')`y_max') aspect(.) ///
		yscale(noextend) xscale(noextend) ///
		legend(off) ///
		ytitle("`t_y'") xtitle("ln(City Emp in ${year1})") ///
		title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("Sample: `t_sample'")
	if c(mode) != "batch" graph export "${dir_outf}/lploy_`png_out'.png", replace width(3000) height(2500)
}
end

local v_city = "msa"
local i_def = 6

capture program drop mgrow_sum
program mgrow_sum
args i_city i_in

global ds_out "${dir_outf}/data/cmgrow_city"
use ${ds_out}, clear
keep if city_code == "`i_city'" & cind_in == `i_in'

gen ln_emp_city_y1 = ln(emp_city_1977)

// Adjust 2007-2013:
replace cmgrow = cmgrow * 5/6 if syear1 == 2007 & syear2 == 2013

// Unadjusted for incumbent alone
if regexm("`i_in'", "1") {
	replace cmgrow = cmgrow * (${sigma}-1)
}

drop if syear1 == 1977 & syear2 == 2013
count
collapse (mean) cmgrow (first) ln_emp_city_y1, by(city) // Average across 5-year periods
count

cmgrow_fig "`i_city'" "cmgrow_`i_city'_s6_in`i_in'" "`i_in'"

save "${dir_outf}/plotting_data_`i_city'_v`i_in'", replace

end


mgrow_sum "msa" "1"
mgrow_sum "msa" "2"

mgrow_sum "fips" "1"
mgrow_sum "fips" "2"

mgrow_sum "czone" "1"
mgrow_sum "czone" "2"

mgrow_sum "msa1983" "1"
mgrow_sum "msa1983" "2"

mgrow_sum "msa1983cz" "1"
mgrow_sum "msa1983cz" "2"


//==============================================================================
// Table

global ds_tab "${dir_outf}/out_agg_mgrow"
clear
save ${ds_tab}, replace emptyok

capture program drop mgrow_sum
program mgrow_sum
args i_city

global ds_out "${dir_outf}/data/cmgrow_city"
use ${ds_out}, clear
keep if city_code == "`i_city'" & cind_in == 2

// Aggregate by city across years, and then aggregate to total with emp weight
drop if syear1 == 1977 & syear2 == 2013
replace cmgrow = cmgrow * 5/6 if syear1 == 2007 & syear2 == 2013
collapse (mean) cmgrow emp_city_2013, by(city)
collapse (mean) cmgrow [w=emp_city_2013]
rounding_4dig cmgrow
gen var = "`i_city'_in2"
append using ${ds_tab}
save ${ds_tab}, replace
end

mgrow_sum "msa" 
mgrow_sum "fips"
mgrow_sum "czone"
mgrow_sum "msa1983"
mgrow_sum "msa1983cz"

use ${ds_tab}, clear
gen city = var
*replace city = "MSA" if regexm(var, "msa")
replace city = "FIPS" if regexm(var, "fips")
order var, first
gen sample = ""
replace sample = "ALL, JOB{1,5}_CINDNEW_M1F1_Y1_Y2, JOB*_Y1_Y2" if regexm(var, "msa")
replace sample = "ALL, JOB{1,5}_CINDNEW_M*F1_Y1_Y2, JOB*_Y1_Y2" if regexm(var, "fips")

keep city cmgrow sample
order city cmgrow sample

*export excel ${dir_outf}/tab_cmgrow_agg.xlsx, sheet("cmgrow") sheetreplace firstrow(var) keepcellfmt
//export excel results/flin/201809/201910_main/Output.xlsx, sheet("cmgrow") sheetreplace firstrow(var) keepcellfmt

di "Ended at $S_DATE $S_TIME"
capture noi log close
// End of do file
